<?php
/**
 * Description of iExamManualclass
 *
 * @author Jeffy Shih
 * @copyright DoRISE team, Science Education center, NTNU.
 * @since 2010-05-14
 * @version 1.0
 */
class Question {

    private $mysqli;

    function __construct() {
        //引入資料庫連結參數設定
        include '../inc/oaConn.inc.php';
        $this->mysqli = @new mysqli($mysqlHost,$mysqlUser,$mysqlPassword,$mysqlDB);
        if(mysqli_connect_errno()) {
            throw new Exception('無法建立資料庫連線！'.mysqli_connect_error());
            $this->mysqli = false;
            exit();
        }

        //連線使用編碼
        $this->mysqli->query("SET NAMES 'utf8'");
    }

    /**
     *
     * @param Array $arySearchCondition
     * @return Array
     */
    public function getQuestionSearchResult($arySearchCondition) {

        $sql_condition = '(`MQGradeBegin`>=' . $arySearchCondition['GradeBegin'] . ' AND `MQGradeEnd`<=' . $arySearchCondition['GradeEnd'] . ')';
        
        if(array_key_exists('SubjectId', $arySearchCondition)) {
            $sql_condition .= ' AND `SId`=' . $arySearchCondition['SubjectId'];
        }

        if(array_key_exists('forPaper', $arySearchCondition)) {
            $sql_condition .= ' AND `MQPaper`=' . $arySearchCondition['forPaper'];
        }

        if(array_key_exists('SchKeyword', $arySearchCondition)) {
            $strSchKeyword = $arySearchCondition['SchKeyword'];
            $sql_condition .= ' AND (`MQTitle` LIKE \'%' . $strSchKeyword . '%\' OR `MQBelief` LIKE \'%' . $strSchKeyword . '%\' OR `MQContent` LIKE \'%' . $strSchKeyword . '%\')';
        }

        $sql_QuestionSearch = 'SELECT * FROM `MainQuestion` WHERE ' . $sql_condition;

        $rs_QuestionSearch = $this->mysqli->query($sql_QuestionSearch);

        if(!$rs_QuestionSearch){
            throw new Exception('搜尋題目時發生錯誤！');
        }else{
            $arySearchResult = array();
            while($row_QuestionSearch = $rs_QuestionSearch->fetch_row()){
                array_push($arySearchResult, $row_QuestionSearch);
            }
            
            return $arySearchResult;
        }
    }

    function  __destruct() {
        if ($this->mysqli) {
            $this->mysqli->close();
            $this->mysqli = FALSE;
        }
    }
}
?>
